package com.shj.controller;

import com.shj.entity.SysArea;
import com.shj.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author songhuajian
 * @Description: TODO
 * @date 2020/5/10 17:55
 **/
@Controller("SysAreaController")
@RequestMapping("/system/area")
public class SysAreaController {
    @Autowired
    private SysAreaService sysAreaService;

    @RequestMapping("/find")
    public String find(Model model) {
        List<SysArea> list = sysAreaService.findAll();
        model.addAttribute("list", list);
        return "area/area_list";
    }

    @RequestMapping("/groupList")
    @ResponseBody
    public List<SysArea> groupList(String villageId) {
        List<SysArea> list = sysAreaService.findChildArea(villageId, 4);
        return list;
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        List<SysArea> list = sysAreaService.findAll();
        model.addAttribute("list", list);
        return "area/area_add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysArea sysArea) {
        sysAreaService.add(sysArea);
        return find(model);
    }

    @RequestMapping("/openEdit")
    public String openEdit(Model model, String code) {
        SysArea sysArea = sysAreaService.findByCode(code);
        model.addAttribute("area", sysArea);
        return "area/area_edit";
    }

    @RequestMapping("/edit")
    public String edit(Model model, SysArea sysArea) {
        sysAreaService.edit(sysArea);
        return find(model);
    }

    @RequestMapping("/delete")
    public String delete(Model model, String code) {
        sysAreaService.delete(code);
        return find(model);
    }
}
